package array

import "sort"

func majorityElement(nums []int) int {
	count := map[int]int{}

	numsLen := len(nums)
	var res int

	for _, v := range nums {
		if _, ok := count[v]; ok {
			count[v]++
		} else {
			count[v] = 1
		}

		if count[v] > numsLen/2 {
			res = v
			break
		}
	}

	return res

}

func majorityElement2(nums []int) int {
	sort.Ints(nums)
	return nums[len(nums)/2]
}
